package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.xgkj.diyiketang.data.MyOrderInfo;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/4/12.
 */
public class OrderAdapter extends BaseAdapter{
    private Context mContext;
    private List<MyOrderInfo> mList = new ArrayList<MyOrderInfo>();

    public OrderAdapter(Context context) {
        mContext= context;
    }

    public void updateData(List<MyOrderInfo> list){
        if (mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null ){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size()>0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() ==null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.order_item,null);
            childHolder.orderStausImage = (ImageView) convertView.findViewById(R.id.order_status);
            childHolder.orderNoText = (TextView) convertView.findViewById(R.id.orderNo);
            childHolder.activityNameText = (TextView) convertView.findViewById(R.id.activity_name);
            childHolder.payMoneyText = (TextView) convertView.findViewById(R.id.pay_money);
            childHolder.createDateText = (TextView) convertView.findViewById(R.id.createdate);
            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        MyOrderInfo info = (MyOrderInfo) mList.get(position);
        String status = info.getStatus();
        if (!TextUtils.isEmpty(status) && status.equals("0")){
         //待付款
            childHolder.orderStausImage.setImageResource(R.drawable.weizhifu);
        }else if (!TextUtils.isEmpty(status) && status.equals("1")){
            //已完成
            childHolder.orderStausImage.setImageResource(R.drawable.yiwancheng);
        }else if (!TextUtils.isEmpty(status) && status.equals("2")){
            //已失效
            childHolder.orderStausImage.setImageResource(R.drawable.yiquxiao);
        }

        String type = info.getType();
        if (type != null && type.equals("0")){
            childHolder.orderNoText.setText("购买花朵:" + info.getOrderNo());
        }else if (type != null && type.equals("1")) {
            childHolder.orderNoText.setText("购买课程:" + info.getOrderNo());
        }else if (type != null && type.equals("2")){
            childHolder.orderNoText.setText("购买vip:"+info.getOrderNo());
        }else if (type != null && type.equals("3")){
            childHolder.orderNoText.setText("购买收费直播:"+info.getOrderNo());
        }else {
            childHolder.orderNoText.setText("订单号:"+info.getOrderNo());
        }
        //childHolder.activityNameText.setText(String.format(mContext.getString(R.string.activity_name),info.getType()));
        childHolder.activityNameText.setText(info.getTitle());
        String price = String .format(mContext.getString(R.string.pay_money),info.getFee());
        SpannableString sp = new SpannableString(price);
        sp.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.hint_color)),0,4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        sp.setSpan(new AbsoluteSizeSpan(14,true),0,4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        sp.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.video_fenlei_font)),4,price.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        sp.setSpan(new AbsoluteSizeSpan(17,true),4,price.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        childHolder.payMoneyText.setText(sp);
        childHolder.createDateText.setText(String.format(mContext.getString(R.string.create_date),info.getCreateDate()));
        return convertView;
    }

    class ChildHolder {
        ImageView orderStausImage;
        TextView orderNoText;
        TextView activityNameText;
        TextView payMoneyText;
        TextView createDateText;
    }
}
